package cn.powerone.scm.service.impl;

import java.util.List;

import cn.powerone.common.exception.ServiceException;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.common.utils.StringUtils;
import cn.powerone.scm.domain.ScmCustomer;
import cn.powerone.scm.mapper.ScmCustomerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmCustomerContactsMapper;
import cn.powerone.scm.domain.ScmCustomerContacts;
import cn.powerone.scm.service.IScmCustomerContactsService;

/**
 * 联系人Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-06-23
 */
@Slf4j
@Service
public class ScmCustomerContactsServiceImpl implements IScmCustomerContactsService 
{
    @Autowired
    private ScmCustomerContactsMapper scmCustomerContactsMapper;
    @Autowired
    private ScmCustomerMapper scmCustomerMapper;

    /**
     * 查询联系人
     * 
     * @param id 联系人主键
     * @return 联系人
     */
    @Override
    public ScmCustomerContacts selectScmCustomerContactsById(Long id)
    {
        return scmCustomerContactsMapper.selectScmCustomerContactsById(id);
    }

    /**
     * 查询联系人列表
     * 
     * @param scmCustomerContacts 联系人
     * @return 联系人
     */
    @Override
    public List<ScmCustomerContacts> selectScmCustomerContactsList(ScmCustomerContacts scmCustomerContacts)
    {
        return scmCustomerContactsMapper.selectScmCustomerContactsList(scmCustomerContacts);
    }

    /**
     * 新增联系人
     * 
     * @param scmCustomerContacts 联系人
     * @return 结果
     */
    @Override
    public int insertScmCustomerContacts(ScmCustomerContacts scmCustomerContacts)
    {
        scmCustomerContacts.setCreateTime(DateUtils.getNowDate());
        return scmCustomerContactsMapper.insertScmCustomerContacts(scmCustomerContacts);
    }

    /**
     * 修改联系人
     * 
     * @param scmCustomerContacts 联系人
     * @return 结果
     */
    @Override
    public int updateScmCustomerContacts(ScmCustomerContacts scmCustomerContacts)
    {
        scmCustomerContacts.setUpdateTime(DateUtils.getNowDate());
        return scmCustomerContactsMapper.updateScmCustomerContacts(scmCustomerContacts);
    }

    /**
     * 批量删除联系人
     * 
     * @param ids 需要删除的联系人主键
     * @return 结果
     */
    @Override
    public int deleteScmCustomerContactsByIds(Long[] ids)
    {
        return scmCustomerContactsMapper.deleteScmCustomerContactsByIds(ids);
    }

    /**
     * 删除联系人信息
     * 
     * @param id 联系人主键
     * @return 结果
     */
    @Override
    public int deleteScmCustomerContactsById(Long id)
    {
        return scmCustomerContactsMapper.deleteScmCustomerContactsById(id);
    }

    /** 导入数据
     * @param scmCustomerContacts 导入询价Batch列表
     */
    @Override
    public String importData(List<ScmCustomerContacts> scmCustomerContacts, boolean updateSupport, String operName)
    {
        if (StringUtils.isNull(scmCustomerContacts) || scmCustomerContacts.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        ScmCustomer customerInq = new ScmCustomer();
        ScmCustomerContacts scmCustomerContactsInq = new ScmCustomerContacts();
        List<ScmCustomer> customerOutList;
        long numberOfResult = 0;
        for (ScmCustomerContacts item : scmCustomerContacts)
        {
            if(item.getCustomerNo() != null && !item.getCustomerNo().isEmpty()){
                customerInq.setCustomerNo(item.getCustomerNo());
                customerOutList = scmCustomerMapper.selectScmCustomerList(customerInq);
                if(customerOutList.isEmpty()){
                    continue;
                }
                item.setCustomerId(customerOutList.get(0).getId());
            }
            if (!updateSupport){
                scmCustomerContactsInq.setCustomerId(item.getCustomerId());
                scmCustomerContactsInq.setContactName(item.getContactName());
                scmCustomerContactsInq.setMobile(item.getMobile());
                numberOfResult = scmCustomerContactsMapper.selectScmCustomerContactsList(scmCustomerContactsInq).size();
                if (numberOfResult > 0 ){
                    continue;
                }
            }
            try
            {
                scmCustomerContactsMapper.insertScmCustomerContacts( item);
                successNum++;
            } catch (Exception e)
            {
                failureNum++;
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }
}
